//
//  CategoryBottomCell.m
//  陶瓷
//
//  Created by 王戈 on 15/6/9.
//  Copyright (c) 2015年 王戈. All rights reserved.
//

#import "CategoryBottomCell.h"
#import "CategoryBtn.h"

@interface CategoryBottomCell ()

@property (nonatomic, strong) CategoryBtn *leftBtn;
@property (nonatomic, strong) CategoryBtn *rightBtn;

@end

@implementation CategoryBottomCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.leftBtn];
        [self.contentView addSubview:self.rightBtn];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"CategoryBottomCell";
    // 1.取缓存中取
    CategoryBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    // 2.创建cell
    if (cell == nil) {
        cell = [[CategoryBottomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    
    //    cell.backgroundColor = [UIColor redColor];
    
    return cell;
}

- (CategoryBtn *)leftBtn {
    if (_leftBtn == nil) {
        _leftBtn = [[CategoryBtn alloc] initWithIsContainIcon:YES];
        _leftBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_leftBtn addTarget:self action:@selector(companyClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftBtn;
}

- (CategoryBtn *)rightBtn {
    if (_rightBtn == nil) {
        _rightBtn = [[CategoryBtn alloc] initWithIsContainIcon:YES];
        _rightBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_rightBtn addTarget:self action:@selector(companyClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightBtn;
}

- (void)setIsNeedIcon:(BOOL)isNeedIcon {
    _isNeedIcon = isNeedIcon;
    
    self.rightBtn.isContainIcon = isNeedIcon;
    
    self.leftBtn.isContainIcon = isNeedIcon;
    
    if (!isNeedIcon) {
        self.rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        self.leftBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.rightBtn.titleLabel setLineBreakMode:NSLineBreakByCharWrapping];
        [self.leftBtn.titleLabel setLineBreakMode:NSLineBreakByCharWrapping];
    }
    else
    {
        self.rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        self.leftBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.rightBtn.titleLabel setLineBreakMode:NSLineBreakByCharWrapping];
        [self.leftBtn.titleLabel setLineBreakMode:NSLineBreakByCharWrapping];

    }
    
    [self layoutIfNeeded];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _leftBtn.frame = CGRectMake(self.width * 0.09, self.height * 0.05, self.width * 0.3, self.height * 0.9);
    _rightBtn.frame = CGRectMake(self.width * 0.58, self.height * 0.05, self.width * 0.3, self.height * 0.9);
}

- (void)setLeftCompany:(CompanyModel *)leftCompany {
    _leftCompany = leftCompany;
    self.leftBtn.company = leftCompany;
}

- (void)setRightCompany:(CompanyModel *)rightCompany {
    _rightCompany = rightCompany;
    self.rightBtn.company = rightCompany;
}

- (void)companyClick:(CategoryBtn *)sender {
    if ([self.delegate respondsToSelector:@selector(categoryBtnClick:)]) {
        [self.delegate categoryBtnClick:sender];
    }
}
@end
